package com.haierp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.haierp.mapper.AutomaticOrderDetailMapper;
import com.haierp.model.automatic.AutomaticOrder;
import com.haierp.model.automatic.AutomaticOrderDetail;
import com.haierp.service.IAutomaticOrderService;
import com.haierp.service.ISequenceUtilService;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.AutomaticQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

/**
 *
 * AutomaticOrder 控制层
 *
 */
@Controller
@RequestMapping("/automaticOrder")
public class AutomaticOrderController extends BaseController {
	@Autowired
	private IAutomaticOrderService automaticOrderService;
	@Autowired
	private ISequenceUtilService sequenceUtilService;
	@Autowired
	private AutomaticOrderDetailMapper automaticOrderDetailMapper;

	/**
	 * 查询
	 */
	@RequestMapping("/queryAutomatic")
	@ResponseBody
	public Object queryAutomatic(AutomaticQueryVO automaticQueryVO) {
		JsonPageResult<List<AutomaticOrder>> result = automaticOrderService.queryAutomatic(automaticQueryVO);
		return result.buildIsSuccess(true);
	}

	/**
	 * 添加
	 */
	@RequestMapping("/addAutomatic")
	@ResponseBody
	public Object addAutomatic(AutomaticOrder automaticOrder) {
		JsonResult<String> result = new JsonResult<>();
		if(automaticOrder.getId() != null) {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		automaticOrder.setAutoNo("ZP"+DateUtil.formatDate(new Date(), DateUtil.DATE_PARTEN_YYMMDDHHMMSS)+"D"+sequenceUtilService.gainAUTOSequence());
		automaticOrder.setGmtCreate(new Date());
		automaticOrder.setGmtModify(new Date());
		String automaticOrderDetailList = automaticOrder.getAutomaticOrderDetailList();
		if (StringUtils.isNotBlank(automaticOrderDetailList)) {
			String s = automaticOrderDetailList.replaceAll("&quot;", "\"");
			List<AutomaticOrderDetail> automaticOrderDetail = HaiJsonUtils.toBean(s, new TypeReference<List<AutomaticOrderDetail>>(){});
			if(CollectionUtils.isEmpty(automaticOrderDetail)) {
				return JsonResult.buildFailed("没有自动下单详情信息");
			}
			automaticOrder.setAutomaticOrderDetail(automaticOrderDetail);
		}
		automaticOrderService.addAutomaticOrder(automaticOrder);
		return result.buildIsSuccess(true);
	}

	/**
	 * 修改查询
	 */
	@RequestMapping("/queryUpdateAutomatic")
	@ResponseBody
	public Object queryUpdateAutomatic(Long automaticId) {
		JsonResult<AutomaticOrder> result = new JsonResult<>();
		AutomaticOrder automaticOrder = automaticOrderService.queryUpdateAutomaticOrder(automaticId);
		return result.buildData(automaticOrder).buildIsSuccess(true);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/UpdateAutomatic")
	@ResponseBody
	public Object UpdateAutomatic(AutomaticOrder automaticOrder) {
		JsonResult<String> result = new JsonResult<>();
		if(automaticOrder.getId() == null) {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		automaticOrder.setGmtModify(new Date());
		String automaticOrderDetailList = automaticOrder.getAutomaticOrderDetailList();
		if (StringUtils.isNotBlank(automaticOrderDetailList)) {
			String s = automaticOrderDetailList.replaceAll("&quot;", "\"");
			List<AutomaticOrderDetail> automaticOrderDetail = HaiJsonUtils.toBean(s, new TypeReference<List<AutomaticOrderDetail>>(){});
			if(CollectionUtils.isEmpty(automaticOrderDetail)) {
				return JsonResult.buildFailed("没有自动下单详情信息");
			}
			automaticOrder.setAutomaticOrderDetail(automaticOrderDetail);
		}
		automaticOrderService.updateAutomaticOrder(automaticOrder);
		return result.buildIsSuccess(true);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/deleteAutomatic")
	@ResponseBody
	public Object deleteAutomatic(Long automaticId) {
		JsonResult<String> result = new JsonResult<>();
		if(automaticId == null) {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		AutomaticOrder automaticOrder = automaticOrderService.selectById(automaticId);
		if(automaticOrder == null) {
			result.buildData("删除自动下单任务不存在").buildIsSuccess(false);
		}
		automaticOrderService.deleteSelective(automaticOrder);
		List<Long> idList = new ArrayList<Long>();
		EntityWrapper<AutomaticOrderDetail> entityWrapper = new EntityWrapper<>();
		entityWrapper.where("automatic_order_id = {0}", automaticId);
		List<AutomaticOrderDetail> automaticOrderDetailList = automaticOrderDetailMapper.selectList(entityWrapper);
		for (AutomaticOrderDetail automaticOrderDetail : automaticOrderDetailList) {
			idList.add(automaticOrderDetail.getId());
		}
		if(idList.size() > 0) {
			automaticOrderDetailMapper.deleteBatchIds(idList);
		}
		return result.buildIsSuccess(true);
	}
}